<?php

namespace C\S\Sys;

use C\L\Service;

class Signin extends Service
{

    protected function setModel()
    {
        $this->model = new \C\M\SysSignin();
    }

    public function getStatusConfig()
    {
        return [
        'type' => [
        1 => '每日',
        2 => '连续'
        ],
        'reward_type' => [
        1 => '红包',
        2 => '代金券',
        ],
        ];
    }

    public function getDetail($data)
    {
        foreach ($data as $k => $v) {
            $where[] = $k . '=' . $v;
        }

        $info = $this->model->findFirst(
            [
            'conditions' => implode(' and ', $where),
            ]
        );

        if (!$info) {
            return [];
        }
        $res = $info->toArray();

        if ($res['reward_type'] == 2) {
            $coupon = $info->coupons;
            $res['coupon']['coupon_id'] = $coupon->id;
            $res['coupon']['coupon_value'] = $coupon->coupon_value;
            $res['coupon']['use_condition'] = $coupon->use_condition;
            $res['coupon']['effective_time'] = $coupon->effective_time;
        }

        return $res;
    }

    public function getContinuityList($conditions, $order = 'days asc')
    {
        $list = $this->model->find(
            [
            'conditions' => 'is_delete=0 and ' . $conditions,
            'order' => $order,
            ]
        );

        if (!$list) {
            return [];
        }
        $res = [];
        foreach ($list as $value) {
            $coupon = $value->coupons;
            if ($coupon) {
                $coupon = $coupon->toArray();
            } else {
                $coupon = [];
            }
            $info = $value->toArray();
            $info['coupon'] = $coupon;
            $res[] = $info;
        }
        return $res;
    }
}
